<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-if="IfShow"
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['index:introduce:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['index:introduce:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['index:introduce:export']"
          >导出</el-button
        >
      </el-col>
      <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="introduceList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="企业文化图片" align="center" prop="picture">
        <template v-slot="{ row }">
          <div
            class="a_b"
            v-if="row.picture && row.picture != ''"
            style="height: 40px; margin: 0 auto; width: 40px"
          >
            <img :src="baseUrl + row.picture" alt="" class="a_b_a" />
            <span class="a_b_b" style="text-align: center">
              <span
                class="a_b_b_a"
                @click="handlePictureCardPreview(row.picture)"
              >
                <el-icon><zoom-in /></el-icon>
              </span>
            </span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="企业介绍图片" align="center" prop="picture1">
        <template v-slot="{ row }">
          <div
            class="a_b"
            v-if="row.picture1 && row.picture1 != ''"
            style="height: 40px; margin: 0 auto; width: 40px"
          >
            <img :src="baseUrl + row.picture1" alt="" class="a_b_a" />
            <span class="a_b_b" style="text-align: center">
              <span
                class="a_b_b_a"
                @click="handlePictureCardPreview(row.picture1)"
              >
                <el-icon><zoom-in /></el-icon>
              </span>
            </span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="企业实力图片" align="center" prop="picture2">
        <template v-slot="{ row }">
          <div
            class="a_b"
            v-if="row.picture2 && row.picture2 != ''"
            style="height: 40px; margin: 0 auto; width: 40px"
          >
            <img :src="baseUrl + row.picture2" alt="" class="a_b_a" />
            <span class="a_b_b" style="text-align: center">
              <span
                class="a_b_b_a"
                @click="handlePictureCardPreview(row.picture2)"
              >
                <el-icon><zoom-in /></el-icon>
              </span>
            </span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="企业愿景图片" align="center" prop="picture3">
        <template v-slot="{ row }">
          <div
            class="a_b"
            v-if="row.picture3 && row.picture3 != ''"
            style="height: 40px; margin: 0 auto; width: 40px"
          >
            <img :src="baseUrl + row.picture3" alt="" class="a_b_a" />
            <span class="a_b_b" style="text-align: center">
              <span
                class="a_b_b_a"
                @click="handlePictureCardPreview(row.picture3)"
              >
                <el-icon><zoom-in /></el-icon>
              </span>
            </span>
          </div>
        </template>
      </el-table-column>
      <!-- <el-table-column label="企业介绍" align="center" prop="introduce" /> -->
      <el-table-column label="企业文化1" align="center" prop="introduce1" />
      <el-table-column label="企业文化2" align="center" prop="introduce2" />
      <el-table-column label="企业文化3" align="center" prop="introduce3" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['index:introduce:edit']"
            >修改</el-button
          >
          <!-- <el-button
            link
            type="primary"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['index:introduce:remove']"
            >删除</el-button
          > -->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改企业文化对话框 -->
    <el-drawer
      :title="title"
      v-model="open"
      size="50%"
      append-to-body
      destroy-on-close
    >
      <el-form
        ref="introduceRef"
        :model="form"
        :rules="rules"
        label-width="120px"
      >
        <el-form-item label="企业图片" prop="picture">
          <div class="a">
            <div class="a_b" v-if="form.picture && form.picture != ''">
              <img :src="baseUrl + form.picture" alt="" class="a_b_a" />
              <span class="a_b_b">
                <span
                  class="a_b_b_a"
                  @click="handlePictureCardPreview(form.picture)"
                >
                  <el-icon><zoom-in /></el-icon>
                </span>
                <span
                  class="a_b_b_b"
                  @click="handleDeletePicture(form.picture, 1)"
                >
                  <el-icon>
                    <Delete />
                  </el-icon>
                </span>
              </span>
            </div>
            <div class="a_a" @click="uploadClick(1)" v-else>
              <el-icon>
                <Plus />
              </el-icon>
              <input
                ref="inputRef1"
                name="file"
                accept=".jpg,.jpeg,.png"
                type="file"
                style="display: none"
                @change="(event) => changeFile(event, 1)"
              />
            </div>
          </div>
        </el-form-item>
        <el-form-item label="企业介绍图片" prop="picture1">
          <div class="a">
            <div class="a_b" v-if="form.picture1 && form.picture1 != ''">
              <img :src="baseUrl + form.picture1" alt="" class="a_b_a" />
              <span class="a_b_b">
                <span
                  class="a_b_b_a"
                  @click="handlePictureCardPreview(form.picture1)"
                >
                  <el-icon><zoom-in /></el-icon>
                </span>
                <span
                  class="a_b_b_b"
                  @click="handleDeletePicture(form.picture1, 2)"
                >
                  <el-icon>
                    <Delete />
                  </el-icon>
                </span>
              </span>
            </div>
            <div class="a_a" @click="uploadClick(2)" v-else>
              <el-icon>
                <Plus />
              </el-icon>
              <input
                ref="inputRef2"
                name="file"
                accept=".jpg,.jpeg,.png"
                type="file"
                style="display: none"
                @change="(event) => changeFile(event, 2)"
              />
            </div>
          </div>
        </el-form-item>
        <el-form-item label="企业实力图片" prop="picture2">
          <div class="a">
            <div class="a_b" v-if="form.picture2 && form.picture2 != ''">
              <img :src="baseUrl + form.picture2" alt="" class="a_b_a" />
              <span class="a_b_b">
                <span
                  class="a_b_b_a"
                  @click="handlePictureCardPreview(form.picture2)"
                >
                  <el-icon><zoom-in /></el-icon>
                </span>
                <span
                  class="a_b_b_b"
                  @click="handleDeletePicture(form.picture2, 3)"
                >
                  <el-icon>
                    <Delete />
                  </el-icon>
                </span>
              </span>
            </div>
            <div class="a_a" @click="uploadClick(3)" v-else>
              <el-icon>
                <Plus />
              </el-icon>
              <input
                ref="inputRef3"
                name="file"
                accept=".jpg,.jpeg,.png"
                type="file"
                style="display: none"
                @change="(event) => changeFile(event, 3)"
              />
            </div>
          </div>
        </el-form-item>
        <el-form-item label="企业愿景图片" prop="picture3">
          <div class="a">
            <div class="a_b" v-if="form.picture3 && form.picture3 != ''">
              <img :src="baseUrl + form.picture3" alt="" class="a_b_a" />
              <span class="a_b_b">
                <span
                  class="a_b_b_a"
                  @click="handlePictureCardPreview(form.picture3)"
                >
                  <el-icon><zoom-in /></el-icon>
                </span>
                <span
                  class="a_b_b_b"
                  @click="handleDeletePicture(form.picture3, 4)"
                >
                  <el-icon>
                    <Delete />
                  </el-icon>
                </span>
              </span>
            </div>
            <div class="a_a" @click="uploadClick(4)" v-else>
              <el-icon>
                <Plus />
              </el-icon>
              <input
                ref="inputRef4"
                name="file"
                accept=".jpg,.jpeg,.png"
                type="file"
                style="display: none"
                @change="(event) => changeFile(event, 4)"
              />
            </div>
          </div>
        </el-form-item>
        <el-form-item label="企业介绍" prop="introduce">
          <wangEditor
            v-model="form.introduce"
            placeholder="请输入内容"
          ></wangEditor>
        </el-form-item>
        <el-form-item label="企业文化1" prop="introduce1">
          <el-input
            v-model="form.introduce1"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item label="企业文化2" prop="introduce2">
          <el-input
            v-model="form.introduce2"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item label="企业文化3" prop="introduce3">
          <el-input
            v-model="form.introduce3"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item label="企业人员" prop="personnelList">
          <div class="tabclass">
            <el-table size="small" border :data="form.personnelList">
              <el-table-column label="照片" align="center" width="80px">
                <template v-slot="{ row, $index }">
                  <el-form-item
                    :prop="'personnelList.' + $index + '.photo'"
                    :rules="rules1.photo"
                  >
                    <div
                      class="a_b"
                      v-if="row.photo && row.photo != ''"
                      style="height: 40px"
                    >
                      <img :src="baseUrl + row.photo" alt="" class="a_b_a" />
                      <span class="a_b_b">
                        <span
                          class="a_b_b_a"
                          @click="handlePictureCardPreview(row.photo)"
                        >
                          <el-icon><zoom-in /></el-icon>
                        </span>
                        <span
                          class="a_b_b_b"
                          @click="handleDeletePicture1(row)"
                        >
                          <el-icon>
                            <Delete />
                          </el-icon>
                        </span>
                      </span>
                    </div>
                    <div
                      class="a_a_z"
                      @click="uploadClick1($index)"
                      v-else
                      style="margin-left: 50%; transform: translateX(-50%)"
                    >
                      <el-icon>
                        <Plus />
                      </el-icon>
                      <input
                        :ref="'tableRef' + $index"
                        name="file"
                        accept=".jpg,.jpeg,.png"
                        type="file"
                        style="display: none"
                        @change="(event) => changeFile1(event, row)"
                      />
                    </div>
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column label="名称" align="center" width="100px">
                <template v-slot="{ row, $index }">
                  <el-form-item
                    :prop="'personnelList.' + $index + '.name'"
                    :rules="rules1.name"
                  >
                    <el-input
                      size="small"
                      v-model="row.name"
                      placeholder="请输入名称"
                    >
                    </el-input
                  ></el-form-item>
                </template>
              </el-table-column>
              <el-table-column label="职位" align="center" width="100px">
                <template v-slot="{ row, $index }">
                  <el-form-item
                    :prop="'personnelList.' + $index + '.posts'"
                    :rules="rules1.posts"
                  >
                    <el-input
                      size="small"
                      v-model="row.posts"
                      placeholder="请输入职位"
                    >
                    </el-input
                  ></el-form-item>
                </template>
              </el-table-column>
              <el-table-column label="简介 " align="center">
                <template v-slot="{ row, $index }">
                  <el-form-item
                    :prop="'personnelList.' + $index + '.brief'"
                    :rules="rules1.brief"
                  >
                    <el-input
                      size="small"
                      v-model="row.brief"
                      type="textarea"
                      placeholder="请输入简介 "
                    >
                    </el-input
                  ></el-form-item>
                </template>
              </el-table-column>
              <el-table-column label="是否启用" align="center" width="105px">
                <template v-slot="{ row, $index }">
                  <el-form-item
                    :prop="'personnelList.' + $index + '.status'"
                    :rules="rules1.status"
                  >
                    <el-switch
                      disabled
                      v-model="row.status"
                      class="ml-2"
                      style="
                        --el-switch-on-color: #13ce66;
                        --el-switch-off-color: #ff4949;
                      "
                      active-value="Y"
                      inactive-value="N"
                      active-text="是"
                      inactive-text="否"
                    />
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column label="操作" align="center" width="60px">
                <template #default="scope">
                  <el-button
                    size="small"
                    type="primary"
                    @click="handleAdds"
                    v-if="scope.$index <= 0"
                    >新增</el-button
                  >
                  <el-button
                    size="small"
                    type="danger"
                    v-if="scope.$index != 0"
                    @click="handleDeletes(scope.row, scope.$index)"
                    >删除</el-button
                  >
                </template>
              </el-table-column>
            </el-table>
          </div>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-drawer>
    <el-dialog v-model="dialogVisible">
      <img style="width: 100%" :src="dialogImageUrl" alt="Preview Image" />
    </el-dialog>
  </div>
</template>

<script setup name="Introduce">
import {
  listIntroduce,
  getIntroduce,
  delIntroduce,
  addIntroduce,
  updateIntroduce,
} from "@/api/index/introduce";
import wangEditor from "@/components/wangEditor";
import { uploadFile } from "@/api/imgupload/index";
const { proxy } = getCurrentInstance();

const introduceList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    picture: null,
    picture1: null,
    picture2: null,
    picture3: null,
    introduce: null,
    introduce1: null,
    introduce2: null,
    introduce3: null,
    valiFlag: null,
  },
  rules: {
    picture: [
      { required: true, message: "企业文化图片不能为空", trigger: "blur" },
    ],
    introduce: [
      { required: true, message: "企业介绍不能为空", trigger: "blur" },
    ],
    introduce1: [
      { required: true, message: "企业文化1不能为空", trigger: "blur" },
    ],
    introduce2: [
      { required: true, message: "企业文化2不能为空", trigger: "blur" },
    ],
    introduce3: [
      { required: true, message: "企业文化3不能为空", trigger: "blur" },
    ],
  },
  rules1: {
    photo: [{ required: true, message: "照片必传", trigger: "blur" }],
    name: [{ required: true, message: "名称不能为空", trigger: "blur" }],
    posts: [{ required: true, message: "职位不能为空", trigger: "blur" }],
    brief: [{ required: true, message: "简介不能为空", trigger: "blur" }],
    // val: [{ required: true, message: "值不能为空", trigger: "blur" }],
  },
});

const { queryParams, form, rules, rules1 } = toRefs(data);
const IfShow = ref(true);
/** 查询企业文化列表 */
function getList() {
  loading.value = true;
  listIntroduce(queryParams.value).then((response) => {
    console.log(response);
    if (response.total > 0) {
      IfShow.value = false;
    }
    introduceList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    picture: null,
    picture1: null,
    picture2: null,
    picture3: null,
    introduce: null,
    introduce1: null,
    introduce2: null,
    introduce3: null,
    createTime: null,
    createBy: null,
    updateTime: null,
    updateBy: null,
    valiFlag: null,
    personnelList: [{}],
  };
  proxy.resetForm("introduceRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}
/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加企业文化";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value;
  getIntroduce(_id).then((response) => {
    form.value = response.data;
    open.value = true;
    title.value = "修改企业文化";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["introduceRef"].validate((valid) => {
    if (valid) {
      if (form.value.id != null) {
        updateIntroduce(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addIntroduce(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

//图片上传
const inputRef1 = ref();
const dialogImageUrl = ref();
const dialogVisible = ref(false);
const baseUrl = import.meta.env.VITE_BASE_URL;
const uploadClick = (type) => {
  proxy.$refs[`inputRef${type}`].click();
};
const changeFile = async (e, type) => {
  let files = e.target.files;
  for (let i = 0; i < files.length; i++) {
    let _file = files[i];
    var file = new FormData();
    file.append("files", _file);
    let res = await uploadFile(file);
    if (res.code == 200) {
      if (type == 1) {
        form.value.picture = res.fileNames;
      }
      if (type == 2) {
        form.value.picture1 = res.fileNames;
      }
      if (type == 3) {
        form.value.picture2 = res.fileNames;
      }
      if (type == 4) {
        form.value.picture3 = res.fileNames;
      }
    }
  }
};
const handlePictureCardPreview = (file) => {
  dialogImageUrl.value = baseUrl + file;
  dialogVisible.value = true;
};
const handleDeletePicture = (file, type) => {
  if (type == 1) {
    form.value.picture = "";
  }
  if (type == 2) {
    form.value.picture1 = "";
  }
  if (type == 3) {
    form.value.picture2 = "";
  }
  if (type == 4) {
    form.value.picture3 = "";
  }
};
//新增列表按钮
const handleAdds = () => {
  form.value.personnelList.push({});
};
// 删除列表按钮
const handleDeletes = (e, index) => {
  form.value.personnelList.splice(index, 1);
};
//表格图片上传
const uploadClick1 = (type) => {
  proxy.$refs[`tableRef${type}`].click();
};
const changeFile1 = async (e, row) => {
  let files = e.target.files;
  for (let i = 0; i < files.length; i++) {
    let _file = files[i];
    var file = new FormData();
    file.append("files", _file);
    let res = await uploadFile(file);
    if (res.code == 200) {
      row.photo = res.fileNames;
    }
  }
};
const handleDeletePicture1 = (row) => {
  row.photo = "";
};
getList();
</script>
<style scoped lang="scss">
.tabclass {
  width: 100%;
  ::v-deep {
    .el-form-item__error {
      display: contents;
    }
  }
}
.a {
  display: flex;
  flex-wrap: wrap;
}

.a_a {
  width: 104px;
  height: 104px;
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
  display: flex;
  align-items: center;
  justify-content: center;

  &:hover {
    border-color: var(--el-color-primary);
  }
}

.a_b {
  margin: 0 8px 8px 0;
  border-radius: 6px;
  width: 104px;
  height: 104px;
  overflow: hidden;
  position: relative;
  border: 1px solid var(--el-border-color);

  .a_b_a {
    width: 100%;
    height: 100%;
    object-fit: cover;
  }

  .a_b_b {
    align-items: center;
    background-color: var(--el-overlay-color-lighter);
    color: #fff;
    cursor: default;
    display: inline-flex;
    font-size: 20px;
    height: 100%;
    justify-content: center;
    left: 0;
    opacity: 0;
    position: absolute;
    top: 0;
    transition: opacity var(--el-transition-duration);
    width: 100%;

    .a_b_b_a {
      cursor: pointer;
      display: none;
    }

    .a_b_b_b {
      color: inherit;
      font-size: inherit;
      position: static;
      cursor: pointer;
      display: none;
      margin-left: 1rem;
    }
  }
}

.a_b:hover {
  .a_b_b {
    opacity: 1;
  }

  .a_b_b_a {
    display: block;
  }

  .a_b_b_b {
    display: block;
  }
}

.a_subTitle {
  font-size: 12px;
  color: red;
  line-height: 1.5;
  margin-top: 4px;
  width: 100%;
}

.b_subTitle {
  font-size: 12px;
  color: rgba(0, 0, 0, 0.45);
  line-height: 1.7;
  margin-top: 4px;
  width: 100%;

  p {
    margin: 0;
    padding: 0;
  }
}

.c {
  display: flex;
  align-items: center;
  margin: 4px 0;
  width: 100%;

  .c_a {
    font-size: 14px;
    color: rgba(0, 0, 0, 0.65);
  }

  .c_b {
    font-size: 14px;
    color: rgba(0, 0, 0, 0.65);
  }
}

.append ::v-deep(.el-input-group__append) {
  padding: 0 10px;
}

.d {
  display: flex;
  align-items: center;
  margin-bottom: 8px;
  width: 100%;

  .d_a {
    font-size: 14px;
    color: rgba(0, 0, 0, 0.65);
  }
}
</style>
